package com.igeek.health.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.health.entity.Role;
import com.igeek.health.entity.User;
import com.igeek.health.entity.UserRole;
import com.igeek.health.exception.HealthException;
import com.igeek.health.mapper.RoleMapper;
import com.igeek.health.mapper.UserMapper;
import com.igeek.health.mapper.UserRoleMapper;
import com.igeek.health.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.health.vo.PageQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fengqq
 * @since 2024-08-26
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper  userRoleMapper;
    @Autowired
    private UserMapper userMapper;


    @Transactional(readOnly = true)
    //分页查询管理员列表
    @Override
    public Page<User> findPage(PageQueryVO pageQueryVO) {
        Page<User> page = new Page<>(pageQueryVO.getCurrentPage(), pageQueryVO.getPageSize());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(pageQueryVO.getQueryString()),User::getUsername,pageQueryVO.getQueryString());
        Page<User> userPage = baseMapper.selectUserRolesPage(page, queryWrapper);
        log.info("userPage:{}",userPage);
        return userPage;
    }

    //根据管理员id查询详情(角色列表)
    @Transactional(readOnly = true)
    @Override
    public List<Role> findAllRoleByUid(Integer id) {
        List<UserRole> userRoles = userRoleMapper.selectList(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, id));
        List<Role> collect =
                userRoles.stream()   //转换为Stream流 类型是UserRole
                        .map(UserRole::getRoleId) //转换为Stream流 类型是Integer 代表的是RoleId
                        .map(roleId -> roleMapper.selectById(roleId)) //转换为Stream流 类型是Role 根据角色id查询对应的角色信息
                        .collect(Collectors.toList()); //Stream转换为 集合
        return collect;
    }

    //根据用户名 查询用户信息(包含角色和权限信息)
    @Transactional(readOnly = true)
    @Override
    public User findUserByUsername(String username) {
        User user = userMapper.selectUserByName(username);
        return user;
    }

}
